﻿using OxyPlot;
using OxyPlot.Annotations;
using OxyPlot.Axes;
using OxyPlot.Series;
using SmartPatientCentre.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SmartPatientCentre.Models
{
    class CreatinineChart : Chart
    {
        public CreatinineChart(List<UniqueResult> resultList, Patient patient)
        {
            Model = new PlotModel();

            Title = "Creatinine";

            // Set up chart
            CustomiseChart(patient);

            // Create a list of DataPoint
            Points = new List<DataPoint>();
            foreach (var result in resultList)
            {
                // Convert DateTime to double for DataPoint
                Points.Add(new DataPoint(DateTimeAxis.ToDouble(result.Date), (double)result.Value));
            }

            // Initialise LineSeries
            Line = new LineSeries()
            {
                Color = OxyColors.Green,
                StrokeThickness = 3,
                MarkerStroke = OxyColors.Green,
                MarkerFill = OxyColors.White,
                MarkerType = MarkerType.Circle,
                MarkerSize = 4,
                MarkerStrokeThickness = 3
            };
            Line.ItemsSource = Points;

            // Add Line to Model
            Model.Series.Add(Line);
        }

        // Method to set up chart
        private void CustomiseChart(Patient patient)
        {
            // Create Date axis
            DateTimeAxis dateAxis = new DateTimeAxis()
            {
                StringFormat = "dd/MM",
                Position = AxisPosition.Bottom,
                IsZoomEnabled = false,
                TickStyle = TickStyle.None,
                IntervalLength = 80
            };
            Model.Axes.Add(dateAxis);

            // Create Value Axis
            LinearAxis valueAxis = new LinearAxis()
            {
                Position = AxisPosition.Right,
                Minimum = 30,
                Maximum = 150,
                IsZoomEnabled = false,
                TickStyle = TickStyle.None
            };
            Model.Axes.Add(valueAxis);

            // Create Horizontal Annotation for Normal Range
            if (patient.Gender == "Male")
            {
                // For Male
                Model.Annotations.Add(new RectangleAnnotation
                {
                    MinimumY = (double)Creatinine.MaleLowerLimit,
                    MaximumY = (double)Creatinine.MaleUpperLimit,
                    Fill = OxyColor.FromAColor(50, OxyColors.Green)
                });
            }
            else
            {
                // For Female
                Model.Annotations.Add(new RectangleAnnotation
                {
                    MinimumY = (double)Creatinine.FemaleLowerLimit,
                    MaximumY = (double)Creatinine.FemaleUpperLimit,
                    Fill = OxyColor.FromAColor(50, OxyColors.Green)
                });
            }
        }
    }
}
